package com.yunqian.exception;

import com.yunqian.common.JSONResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;
import java.util.Objects;

/**
 * web层抛出的异常统一处理类
 */
@ControllerAdvice
@Log4j2
public class MyExceptionHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public JSONResult processValidationError(MethodArgumentNotValidException ex) {
        BindingResult result = ex.getBindingResult();
        List<FieldError> fieldErrors = result.getFieldErrors();

        return processFieldErrors(fieldErrors);
    }

    private JSONResult processFieldErrors(List<FieldError> fieldErrors) {
        JSONResult dto = Objects.nonNull(fieldErrors.get(0))
                ? JSONResult.serverError(fieldErrors.get(0).getDefaultMessage())
                : JSONResult.serverError(null);

        for (FieldError fieldError : fieldErrors) {
            dto.addValidationError(fieldError);
        }

        return dto;
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ResponseBody
    public JSONResult processAccessDeniedException(AccessDeniedException ex) {
        log.error("访问拒绝" + ex.getMessage(),ex);
        return JSONResult.forbidden();
    }


    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public JSONResult processRuntimeException(AuthenticationException ex) {
        log.error("认证失败" + ex.getMessage(),ex);
        return JSONResult.unauthorized(ex.getMessage());
    }

    /**
     * 服务层业务错误
     * @param ex
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public JSONResult serviceException(Exception ex) {
        log.error("服务层异常" + ex.getMessage(),ex);
        return JSONResult.serverError(ex.getMessage());
    }

    /**
     * 控制层业务错误
     * @param ex
     * @return
     */
    @ExceptionHandler(ActionException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public JSONResult actionException(Exception ex) {
        log.error("控制层异常" + ex.getMessage(),ex);
        return JSONResult.serverError(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public JSONResult processRuntimeException(Exception ex) {
        log.error("程序错误" + ex.getMessage(),ex);
        return JSONResult.serverError(ex.getMessage());
    }
}